// TestClientManager.cpp - Attaches and tests linux advanced sound architecture library

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <unistd.h>
#include "RingBuffer.h"
#include <stdio.h>
#include "DebugLogger.h"

int main(int argc, char **argv)
{
	unsigned char pBuf[10];

	goto THISTEST;


	if ( 1){
		fprintf(stderr, "\nTest%i: //Write 5 chars\n", __LINE__);
		RingBuffer ringbuffer(10);
		unsigned char pData1[] = "\001\002\003\004\005";
		ringbuffer.Write( pData1, 5);
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: //Write 5 chars x 2\n", __LINE__);
		RingBuffer ringbuffer(10);
		unsigned char pData1[] = "\001\002\003\004\005";
		ringbuffer.Write( pData1, 5);
		ringbuffer.Write( pData1, 5);
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: //Write 5 chars x 3\n", __LINE__);
		RingBuffer ringbuffer(10);
		unsigned char pData1[] = "\001\002\003\004\005";
		ringbuffer.Write( pData1, 5);
		ringbuffer.Write( pData1, 5);
		ringbuffer.Write( pData1, 5);
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: //Write 5 chars x 3. Wrap around write\n", __LINE__);
		RingBuffer ringbuffer(12);
		unsigned char pData1[] = "\001\002\003\004\005";
		ringbuffer.Write( pData1, 5);
		ringbuffer.Write( pData1, 5);
		ringbuffer.Write( pData1, 5);
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: //write ABCDEABABCDEABCDEABCDE. 1 char_per_read threaded. \n", __LINE__);
		RingBuffer ringbuffer(10);
		ringbuffer.InitBufferFileName("ABCDEABABCDEABCDEABCDE-1.raw");
		ringbuffer.SetReadSize(1);

		ringbuffer.StartReadingThread();
		ringbuffer.ResumeReading();

		//unsigned char pData1[] = "\001\002\003\004\005";
		unsigned char pData1[] = "ABCDE";
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 2\n");
		ringbuffer.Write( pData1, 2);
		DEBUGLOG0("Sleep 2 ................................\n");
		sleep(2);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Sleep 2 ................................\n");
		sleep(2);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Sleep 10 ................................\n");
		sleep(10);
		ringbuffer.Exit();
		DEBUGLOG2("exit %s %i\n", __FILE__, __LINE__);
		//fprintf(stderr, "Read %i", ringbuffer.Read(pBuf, 2));
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: //write ABCDEABABCDEABCDEABCDE. 2 char_per_read threaded. \n", __LINE__);
		RingBuffer ringbuffer(10);
		ringbuffer.InitBufferFileName("ABCDEABABCDEABCDEABCDE-2.raw");
		ringbuffer.SetReadSize(2);

		ringbuffer.StartReadingThread();
		ringbuffer.ResumeReading();

		//unsigned char pData1[] = "\001\002\003\004\005";
		unsigned char pData1[] = "ABCDE";
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 2\n");
		ringbuffer.Write( pData1, 2);
		DEBUGLOG0("Sleep 2 ................................\n");
		sleep(2);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Sleep 2 ................................\n");
		sleep(2);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Sleep 10 ................................\n");
		sleep(5);
		ringbuffer.Exit();
		DEBUGLOG2("exit %s %i\n", __FILE__, __LINE__);
		//fprintf(stderr, "Read %i", ringbuffer.Read(pBuf, 2));
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: //write ABCDEAB. start reading. write ABCDEABCDEABCDE. 2 char_per_read threaded. \n", __LINE__);
		RingBuffer ringbuffer(10);
		ringbuffer.InitBufferFileName("ABCDEABCDEABCDE-3.raw");
		ringbuffer.SetReadSize(2);

		ringbuffer.StartReadingThread();

		//unsigned char pData1[] = "\001\002\003\004\005";
		unsigned char pData1[] = "ABCDE";
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 2\n");
		ringbuffer.Write( pData1, 2);
		ringbuffer.ResumeReading();
		DEBUGLOG0("Sleep 2 ................................\n");
		sleep(2);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Sleep 2 ................................\n");
		sleep(2);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Sleep 10 ................................\n");
		sleep(5);
		ringbuffer.Exit();
		DEBUGLOG2("exit %s %i\n", __FILE__, __LINE__);
		//fprintf(stderr, "Read %i", ringbuffer.Read(pBuf, 2));
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: //write ABCDEAB. start reading. write ABCDEABCDEABCDE. 2 char_per_read threaded. \n", __LINE__);
		RingBuffer ringbuffer(10);
		ringbuffer.InitBufferFileName("ABCDEABCDEABCDE-4.raw");
		ringbuffer.SetReadSize(2);

		ringbuffer.StartReadingThread();
		ringbuffer.ResumeReading();

		//unsigned char pData1[] = "\001\002\003\004\005";
		unsigned char pData1[] = "ABCDE";
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 2\n");
		ringbuffer.Write( pData1, 2);
		ringbuffer.ResumeReading();
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		DEBUGLOG0("Write 5\n");
		ringbuffer.Write( pData1, 5);
		ringbuffer.Exit();
		DEBUGLOG2("exit %s %i\n", __FILE__, __LINE__);
		//fprintf(stderr, "Read %i", ringbuffer.Read(pBuf, 2));
	}


	if ( 1){
		fprintf(stderr, "\nTest%i: //write ABCDEAB. start reading. write ABCDEABCDEABCDE. 2 char_per_read threaded. \n", __LINE__);
		RingBuffer ringbuffer(10);
		ringbuffer.InitBufferFileName("ABCDEABCDEABCDE-5.raw");
		ringbuffer.SetReadSize(1);

		ringbuffer.StartReadingThread();
		ringbuffer.ResumeReading();

		//unsigned char pData1[] = "\001\002\003\004\005";
		unsigned char pData1[] = "ABCDEFGHIJ";
		for ( int i=0; i<11; i++) {
			DEBUGLOG2("Write %i %i\n", i, i%11);
			ringbuffer.Write( pData1, i%11);
		}
		DEBUGLOG0("Sleeping for 2sec to let the reading thread to consume more\n");
		sleep(2);
		ringbuffer.Exit();
		DEBUGLOG2("exit %s %i\n", __FILE__, __LINE__);
		//fprintf(stderr, "Read %i", ringbuffer.Read(pBuf, 2));
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: // \n", __LINE__);
		RingBuffer ringbuffer(10);
		ringbuffer.InitBufferFileName("ABCDEABCDEABCDE-6.raw");
		ringbuffer.SetReadSize(1);

		ringbuffer.StartReadingThread();
		ringbuffer.ResumeReading();

		unsigned char pData1[] = "ABCDEFGHIJ";
		unsigned char pReadBuf[20];
		ringbuffer.Write( pData1, 10);
		int i = ringbuffer.ReadPrevBuffer( pReadBuf, 20);
		i ? pReadBuf[i-1] = '\0' : pReadBuf[0] = '\0';
		DEBUGLOG2("ReadPrev 20. Got %i: %s\n", i, pReadBuf);

		//ringbuffer.Write( pData1, 9);

		ringbuffer.Exit();
		DEBUGLOG0("Test End\n");
		//fprintf(stderr, "Read %i", ringbuffer.Read(pBuf, 2));
	}

	THISTEST:

	if ( 1){
		fprintf(stderr, "\nTest%i: // \n", __LINE__);
		RingBuffer ringbuffer(10);
		ringbuffer.InitBufferFileName("ABCDEABCDEABCDE-7.raw");
		ringbuffer.SetReadSize(1);

		unsigned char pData1[] = "ABCDEFGHIJ";
		unsigned char pReadBuf[20];
		ringbuffer.Write( pData1, 9);
		int i = ringbuffer.ReadPrevBuffer( pReadBuf, 20);
		i ? pReadBuf[i] = '\0' : pReadBuf[0] = '\0';
		DEBUGLOG2("ReadPrev 20. Got %i: %s\n", i, pReadBuf);

		ringbuffer.Exit();
		DEBUGLOG0("Test End\n");
		//fprintf(stderr, "Read %i", ringbuffer.Read(pBuf, 2));
	}

	if ( 1){
		fprintf(stderr, "\nTest%i: // \n", __LINE__);
		RingBuffer ringbuffer(10);
		ringbuffer.InitBufferFileName("ABCDEABCDEABCDE-7.raw");
		ringbuffer.SetReadSize(1);

		unsigned char pData1[] = "ABCDEFGHIJ";
		unsigned char pReadBuf[20];
		ringbuffer.Write( pData1, 9);
		ringbuffer.Write( pData1, 9);

		for ( int i=0; i<12; i++) {
			int iRead = ringbuffer.ReadPrevBuffer( pReadBuf, i);
			iRead ? pReadBuf[iRead] = '\0' : pReadBuf[0] = '\0';
			DEBUGLOG3("ReadPrev %i Got %i: %s\n", i, iRead, pReadBuf);
		}

		ringbuffer.Exit();
		DEBUGLOG0("Test End\n");
		//fprintf(stderr, "Read %i", ringbuffer.Read(pBuf, 2));
	}
}
